package a.a;

import a.b.c.SectionCursorAdatper;
import a.db.MyDatabase;
import a.db.MyDatabase.SectionTest;
import a.db.MyDatabase.Test;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.RecyclerListener;
import android.widget.ListView;
import android.widget.TextView;

public class SectionableAdapterTutorialActivity extends ListActivity implements RecyclerListener {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// for (int i = 0; i < 500; i++) {
		// insertData();
		// }

		Cursor c = managedQuery(MyDatabase.Test.CONTENT_URI, null, null, null, Test.NAME);
		getListView().setRecyclerListener(this);
		setListAdapter(new SectionCursorAdatper(this, R.layout.section, new NormalAdapter(this, R.layout.item, c), Test.NAME) {
			@Override
			protected void bindSectionView(View view, Context context, Cursor cursor) {
				String text = cursor.getString(cursor.getColumnIndex(SectionTest.NAME));
				TextView tv = (TextView) view.findViewById(R.id.text);
				tv.setText(text+"   *");
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("section adapter tutorial");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		startActivity(new Intent(this, SectionAdapterTutorialActivity.class));
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onMovedToScrapHeap(View v) {
		Log.d("TAG", "onMovedToScrapHeap()" + v);
	};

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.d("TAG", "onListItemClick()" + position);
	}

	class NormalAdapter extends ResourceCursorAdapter {

		public NormalAdapter(Context context, int layout, Cursor c) {
			super(context, layout, c);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			String text = cursor.getString(cursor.getColumnIndex(SectionTest.NAME));
			TextView tv = (TextView) view.findViewById(R.id.text);
			tv.setText(text);
		}

	}

	private void insertData() {
		ContentResolver resolver = getContentResolver();

		ContentValues values = new ContentValues();
		values.put(SectionTest.NAME, "1");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "2");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "A");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, " left space1");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "B");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "あ");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "朝");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "ア");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "あ");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "ア");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, " left space2");
		resolver.insert(Test.CONTENT_URI, values);

		getListView().setCacheColorHint(Color.TRANSPARENT);
		getListView().setItemsCanFocus(true);
		getListView().setRecyclerListener(this);
	}
}